from spargameproject.spargame.game.engine.game_engine import PlayHandler
from spargameproject.spargame.models import GamePlayer
from spargameproject.spargame.game.playhandlers.spar_play_handlers import getActiveGamePlayersWithoutObserversAndLosers

class ReadyHandler(PlayHandler):
  def getType(self):
    return 'ready'

  def handlePlay(self, play, game_engine):
    session = play.game.getMetadata('session')
    play.setMetadata('session', session)
    print 'Just handled a play of type: ' + self.getType()
    game_player = GamePlayer.objects.get(player=play.player, game=play.game)
    game_player.setMetadata('ready', True)
    message = 'Ready for this round'
    response = {'message':message}
    for game_player in getActiveGamePlayersWithoutObserversAndLosers(play.game):
      if not game_player.getMetadata('ready'):
        return [], response
    play.game.setMetadata('ready', True)
    play.setMetadata('message', message)
    return [], response
